import { IParam, IUIService } from '../interface';

export class AuthUtil {
	/**
	 * 计算界面行为权限状态
	 *
	 * @export
	 * @param {IParam} data 数据
	 * @param {IParam} ActionModel 行为模型
	 * @param {*} UIService UI服务
	 * @return {*}
	 */
	static calcActionItemAuthState(
		data: IParam,
		ActionModel: IParam,
		UIService: IUIService,
        viewModel?: IParam
	) {
		const result: number[] = [];
		if (UIService) {
			for (const key in ActionModel) {
				if (ActionModel.hasOwnProperty(key)) {
					const item = ActionModel[key];
					let dataActionResult: any;
					if (item && item['dataAccessAction']) {
						if (
							Object.is(item['actionTarget'], 'NONE') ||
							Object.is(item['actionTarget'], '')
						) {
							dataActionResult = UIService.getOPPrivsAuth(
								item['dataAccessAction'],
								{}
							);
						} else {
							if (data && Object.keys(data).length > 0) {
								dataActionResult = UIService.getOPPrivsAuth(
									item['dataAccessAction'],
									data
								);
							}
						}
					} else {
                        if (viewModel && App.getEnableAppPermission()) {
                            const appName = App.getAppConfig().appName;
                            const sysName = App.getAppConfig().sysName;
                            dataActionResult = App.getAppUniresAuth(`${sysName}-${appName}-${viewModel.codeName}-${item.codeName}`) ? 1 : 0;
                        } else {
                            dataActionResult = 1;
                        }
					}
					// 无权限:0;有权限:1
					if (dataActionResult === 0) {
						// 禁用:1;隐藏:2;隐藏且默认隐藏:6
						if (item.noPrivDisplayMode === 1) {
							item.disabled = true;
						}
						if (item.noPrivDisplayMode === 2 || item.noPrivDisplayMode === 6) {
							item.visible = false;
						} else {
							item.visible = item.visible && true;
						}
					}
					if (dataActionResult === 1) {
						item.visible = item.visible && true;
						item.disabled = item.disabled || false;
					}
					// 返回权限验证的结果
					item.dataActionResult = dataActionResult;
					result.push(dataActionResult);
				}
			}
		}
		return result;
	}

	/**
	 * 计算工具栏项状态
	 *
	 * @export
	 * @param {boolean} state 状态值
	 * @param {IParam[]} toolbarItems 工具栏项集合
	 */
	static calcToolbarItemState(state: boolean, toolbarItems: IParam[]) {
		const computeItemState = (models: any) => {
			for (const key in models) {
				if (models.hasOwnProperty(key)) {
					const item = models[key];
					if (
						item.uiAction &&
						(Object.is(item.uiAction.actionTarget, 'SINGLEKEY') ||
							Object.is(item.uiAction.actionTarget, 'MULTIKEY') ||
							Object.is(item.uiAction.actionTarget, 'SINGLEDATA'))
					) {
						item.disabled = state;
					}
					if(!item.hasOwnProperty('visible')){
						item.visible = true ;
					}
					if (item.noPrivDisplayMode && item.noPrivDisplayMode === 6) {
						item.visible = !state;
					}
					if (item.itemType === 'ITEMS' && item.items.length > 0) {
						computeItemState(item.items);
					}
				}
			}
		};
		computeItemState(toolbarItems);
	}

	/**
	 * 计算工具栏权限状态
	 *
	 * @export
	 * @param {IParam} data 数据
	 * @param {IParam[]} toolbarItems 工具栏项集合
	 * @param {*} UIService UI服务
	 * @return {*}
	 */
	static calcToolbarItemAuthState(
		data: IParam,
		toolbarItems: IParam[],
		UIService: IUIService,
        viewModel?: IParam
	) {
		const computeAuthState = (models: IParam[]) => {
			for (const item of models) {
                let dataActionResult: any;
                if (item && item['dataAccessAction']) {
                    // 不需要数据的界面行为
                    if (
                        item.uiAction &&
                        (Object.is(item.uiAction.actionTarget, 'NONE') ||
                            !item.uiAction.actionTarget)
                    ) {
                        if (
                            !item.uiAction.actionTarget &&
                            Object.is(item.uiAction.uIActionTag, 'Save') &&
                            UIService.isEnableDEMainState
                        ) {
                            if (data && Object.keys(data).length > 0) {
                                dataActionResult = UIService.getOPPrivsAuth(
                                    item['dataAccessAction'],
                                    data
                                );
                            }
                        } else {
                            dataActionResult = UIService.getOPPrivsAuth(
                                item['dataAccessAction'],
                                {}
                            );
                        }
                    } else {
                        if (
                            data &&
                            Object.keys(data).length > 0 &&
                            UIService.isEnableDEMainState
                        ) {
                            dataActionResult = UIService.getOPPrivsAuth(
                                item['dataAccessAction'],
                                data
                            );
                        } else {
                            dataActionResult = UIService.getOPPrivsAuth(
                                item['dataAccessAction'],
                                {}
                            );
                        }
                    }
                } else {
                    if (viewModel && item.uiAction && App.getEnableAppPermission()) {
                        const appName = App.getAppConfig().appName;
                        const sysName = App.getAppConfig().sysName;
                        const authTag = `${sysName}-${appName}-${viewModel.codeName}-${item.uiAction.codeName}`;
                        dataActionResult = App.getAppUniresAuth(authTag) ? 1 : 0;
                    } else {
                        dataActionResult = 1;
                    }
                }
                // 无权限:0;有权限:1
                if (dataActionResult === 0) {
                    // 禁用:1;隐藏:2;隐藏且默认隐藏:6
                    if (item.noPrivDisplayMode === 1) {
                        item.disabled = true && item.disabled;
                    } else {
                        item.visible = false;
                    }
                }
                if (dataActionResult === 1) {
                    item.visible = true && item.visible;
                    item.disabled = item.disabled || false;
                }
                if (item.itemType === 'ITEMS' && item.items.length > 0) {
                    computeAuthState(item.items);
                }
			}
		};
		if (UIService) {
			computeAuthState(toolbarItems);
		}
	}
}
